<#
.SYNOPSIS
    Automatically creates GetX page, controller and binding files for Flutter projects.
.DESCRIPTION
    This script creates a new module directory with page.dart, controller.dart and binding.dart files
    following the GetX architecture pattern, and optionally generates routes.
#>

param (
    [Parameter(Mandatory=$true)]
    [string]$ModuleName,

    [string]$SubDirectory,

    [string]$ProjectPath = ".",

    [switch]$SkipRouteGeneration
)

# ===== 配置区域 =====
$PackageName = "vischat"
# ===================

# 验证项目路径
if (-not (Test-Path -Path $ProjectPath)) {
    Write-Host "Error: Project path '$ProjectPath' does not exist." -ForegroundColor Red
    exit 1
}

# 验证lib目录是否存在
$LibPath = Join-Path -Path $ProjectPath -ChildPath "lib"
if (-not (Test-Path -Path $LibPath)) {
    Write-Host "Error: 'lib' directory not found in project path." -ForegroundColor Red
    exit 1
}

# 转换模块名称为首字母大写
$ModuleCapitalized = (Get-Culture).TextInfo.ToTitleCase($ModuleName.ToLower())

# 设置基础路径
$ModulesPath = Join-Path -Path $LibPath -ChildPath "modules"

# 如果有子目录参数，则添加到路径中
if (-not [string]::IsNullOrEmpty($SubDirectory)) {
    $ModulesPath = Join-Path -Path $ModulesPath -ChildPath $SubDirectory
}

# 最终模块路径
$ModulePath = Join-Path -Path $ModulesPath -ChildPath $ModuleName

# 创建目录
try {
    if (-not (Test-Path -Path $ModulesPath)) {
        New-Item -ItemType Directory -Path $ModulesPath -Force | Out-Null
        Write-Host "Created directory: $ModulesPath" -ForegroundColor Green
    }

    if (-not (Test-Path -Path $ModulePath)) {
        New-Item -ItemType Directory -Path $ModulePath -Force | Out-Null
        Write-Host "Created directory: $ModulePath" -ForegroundColor Green
    }
}
catch {
    Write-Host "Error creating directories: $_" -ForegroundColor Red
    exit 1
}

# 构建模块的完整路径用于import
$ModuleImportPath = "modules"
if (-not [string]::IsNullOrEmpty($SubDirectory)) {
    $ModuleImportPath += "/$SubDirectory"
}
$ModuleImportPath += "/$ModuleName"

# 创建controller文件
$ControllerContent = @"
import 'package:get/get.dart';

class ${ModuleCapitalized}Controller extends GetxController {

}
"@

$ControllerPath = Join-Path -Path $ModulePath -ChildPath "${ModuleName}_controller.dart"
try {
    $ControllerContent | Out-File -FilePath $ControllerPath -Encoding utf8
    Write-Host "Created controller: $ControllerPath" -ForegroundColor Green
}
catch {
    Write-Host "Error creating controller file: $_" -ForegroundColor Red
    exit 1
}

# 创建binding文件
$BindingContent = @"
import 'package:get/get.dart';
import 'package:${PackageName}/${ModuleImportPath}/${ModuleName}_controller.dart';

class ${ModuleCapitalized}Binding extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut(() => ${ModuleCapitalized}Controller());
  }
}
"@

$BindingPath = Join-Path -Path $ModulePath -ChildPath "${ModuleName}_binding.dart"
try {
    $BindingContent | Out-File -FilePath $BindingPath -Encoding utf8
    Write-Host "Created binding: $BindingPath" -ForegroundColor Green
}
catch {
    Write-Host "Error creating binding file: $_" -ForegroundColor Red
    exit 1
}

# 创建page文件
$PageContent = @"
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:${PackageName}/core/annotations/getx_route_enhanced.dart';
import 'package:${PackageName}/${ModuleImportPath}/${ModuleName}_controller.dart';
import 'package:${PackageName}/${ModuleImportPath}/${ModuleName}_binding.dart';

@GetXRoute(
  '/${ModuleName}',
  binding: ${ModuleCapitalized}Binding,
)
class ${ModuleCapitalized}Page extends GetView<${ModuleCapitalized}Controller> {
  const ${ModuleCapitalized}Page({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold();
  }
}
"@

$PagePath = Join-Path -Path $ModulePath -ChildPath "${ModuleName}_page.dart"
try {
    $PageContent | Out-File -FilePath $PagePath -Encoding utf8
    Write-Host "Created page: $PagePath" -ForegroundColor Green
}
catch {
    Write-Host "Error creating page file: $_" -ForegroundColor Red
    exit 1
}

# 执行路由生成脚本
if (-not $SkipRouteGeneration) {
    $GenerateRoutesPath = Join-Path -Path $ProjectPath -ChildPath "scripts\generate_routes.ps1"

    if (Test-Path -Path $GenerateRoutesPath) {
        try {
            Write-Host "`nExecuting generate_routes.ps1..." -ForegroundColor Cyan
            & $GenerateRoutesPath
            Write-Host "Route generation completed." -ForegroundColor Green
        }
        catch {
            Write-Host "Error executing generate_routes.ps1: $_" -ForegroundColor Yellow
        }
    }
    else {
        Write-Host "Warning: generate_routes.ps1 not found at $GenerateRoutesPath" -ForegroundColor Yellow
    }
}

Write-Host "`nGetX module '$ModuleName' created successfully!" -ForegroundColor Cyan